package morning.cat.springdatademo.controller;

import morning.cat.springdatademo.entity.User;
import morning.cat.springdatademo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.*;
import java.util.List;

/**
 * @describe: 类描述信息
 * @author: morningcat.zhang
 * @date: 2019/2/17 11:45 PM
 */
@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private UserRepository userRepository;

    /**
     * http://127.0.0.1:8080/data/hello/by?name=zhangsan
     *
     * @param name
     * @return
     */
    @RequestMapping("/by")
    public User name(@RequestParam String name) {
        return userRepository.findByName(name);
    }

    @RequestMapping("/by2")
    public List<User> hello() {

        Specification specification = new Specification() {

            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {

//                Path path = root.get("age");
//                return criteriaBuilder.lt(path, 30);


                //query.groupBy(root.get("name").as(String.class));
                return query.getRestriction();

            }
        };

        List<User> userList = userRepository.findAll(specification);

        return userList;
    }
}
